package server

import (
	"github.com/simplechain-org/client/log"
	"os"
	"path"
)

type Server interface {
	Run(configPath string) error
	Close() error
}

func SetLogConfig(logConfig *log.Config) {
	if logConfig != nil {
		if logConfig.WriteFile {
			logFilePath := ""
			if logConfig.FileRoot == "" {
				dir, err := os.Getwd()
				if err == nil {
					logFilePath = path.Join(dir, logConfig.FilePath)
				}
			} else {
				logFilePath = path.Join(logConfig.FileRoot, logConfig.FilePath)
			}
			log.Root().SetHandler(log.NewFileLvlHandler(logFilePath, logConfig.MaxBytesSize, logConfig.Level))
		} else {
			var logger *log.GlogHandler
			logger = log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(true)))
			logLevel, err := log.LvlFromString(logConfig.Level)
			if err != nil {
				panic(err)
			}
			logger.Verbosity(logLevel)
			log.Root().SetHandler(logger)
		}
	}
}

//TransactionVO 交易视图对象
type TransactionVO struct {
	Id               uint64 `json:"id"               `    //id
	TransactionIndex string `json:"transaction_index"`    //交易索引
	Hash             string `json:"tx_hash"             ` //交易hash
	BlockNumber      string `json:"block_number"     `    //区块编号
	BlockHash        string `json:"block_hash"       `    //区块hash
	Time             string `json:"timestamp"        `    //时间戳
	From             string `json:"from"             `    //链名称
	Recipient        string `json:"to"               `    //接收地址
	Amount           string `json:"value"            `    //金额
	AccountNonce     string `json:"nonce"            `    //随机数
	Price            string `json:"gas_price"        `    //gas价格
	GasLimit         string `json:"gas"              `    //gas限额
	Payload          string `json:"input"            `    //携带数据
	V                string `json:"v"                `    //签名v
	R                string `json:"r"                `    //签名r
	S                string `json:"s"                `    //签名s
	Status           int    `json:"status"           `    //交易验证状态
}

type AesDecryptReq struct {
	CipherText string `json:"cipherText"` //密文
	AesKey     string `json:"aesKey"`     //RSA加密密钥(至少1024bit)
}

type AesDecryptVO struct {
	PlainText string `json:"plainText"` //原文
}
